# TODO: Add comment
# 
# Author: rogb
###############################################################################

setClass("Swap",
		representation=list(
		),contains=c("FloatPaymentFrequency","SwapRate","Nominal")
)

Swap <- function(EffectiveDate,MaturityDate,Rate,FixPaymentFrequency,Currency,Name,SituationDate,DayCountConvention,Exchange,Nominal){
	if(missing(SituationDate)){
		SituationDate <- Sys.Date()
	}
	if(missing(Exchange)){
		Exchange <- "DEFAULTEXCHANGE"
	}
	
	if(Currency=="GBP"){
		EffectiveDate <- SituationDate
	}
	Term <- round(yearFraction(EffectiveDate,MaturityDate))
	new("Swap",Term=Term,Rate=Rate,FixPaymentFrequency=FixPaymentFrequency,Currency=Currency,Name=Name,SituationDate=SituationDate,EffectiveDate=EffectiveDate,MaturityDate=MaturityDate,DayCountConvention=DayCountConvention,Exchange=Exchange,Nominal=Nominal)
}


# EffectiveDate <- DateYMD(2010,11,30)
# MaturityDate <- DateYMD(2020,12,15)
# Rate <- 5
# FixPaymentFrequency <- 1
# Currency <- "CHF"
# Name <- "Swap Rate"
# SituationDate <- Sys.Date()
# DayCountConvention <- "30/360"
# Exchange <- "SWISSEXCHANGE"
# Nominal <- 250000000

# Sw1 <- Swap(EffectiveDate,MaturityDate,Rate,FixPaymentFrequency,Currency,Name,SituationDate,DayCountConvention,Exchange,Nominal)
	
setMethod("show","Swap",function(object){
			cat("Swap\n")
			cat(object@Currency," ",object@Name," ",situationDate.character(object),"\n",sep="")
			cat("Effective Date: ",effectiveDate.character(object),"\n",sep="")
			cat("Term:           ",object@Term,"\n",sep="")
			cat("Frequency:      ",object@FixPaymentFrequency,"\n",sep="")
			cat("Rate:           ",object@Rate,"\n",sep="")
			cat("Nominal:        ",cashNumberToText(object@Nominal),"\n",sep="")
		})

setMethod("generateCashFlows","Swap",function(object){
			EffectiveDate <- object@EffectiveDate
			MaturityDate <- object@MaturityDate
			officialInterestDates <- monthlyDateSequence(EffectiveDate,MaturityDate,object@FixPaymentFrequency,Direction=-1,overlapping=FALSE,keepEndOfMonth=TRUE)
			interestPaymentDates <- getTradingDay(officialInterestDates,Exchange=object@Exchange,Direction=1)
			n <- length(interestPaymentDates)
			CashFlow <- accruedInterest(object@Rate,object@DayCountConvention,object@FixPaymentFrequency,interestPaymentDates[1:(n-1)],interestPaymentDates[2:n],interestPaymentDates[2:n])
			CashFlow[n-1] <- CashFlow[n-1] + 100
			CashFlow <- c(-100,CashFlow)
			CashFlow(object@Currency,object@Name,interestPaymentDates,CashFlow)*object@Nominal / 100
		})

# generateCashFlows(Sw1)
#Rate <- estimateIRR(Sw1)
#CF <- generateCashFlows(Sw1)
#n <- dim(CF)[1]
#MaturityDate <- getDate(CF)[n]
#Currency <- currency(Sw1)
#SituationDate <- situationDate(Sw1)
#Date <- getDate(CF)
#ZR <- ZeroRateCurve(SituationDate,Currency,object@Name,c(SituationDate,Date),rep(Rate,n+1))
#presentValue(Sw1,ZR)




